#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  :search_shop_supply_map_api.py
# Time  :2024/12/13 9:46
# Email :fortunatedong@163.com
# Author:by Adonli
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  :get_service_inspection_serch_api.py
# Time  :2024/9/20 11:50
# Email :fortunatedong@163.com
# Author:by Adonli
import requests
import datetime
import json
import hashlib

class SearchShopSupplyMap:
    def __init__(self,sku):
        """
        @param method: 配置的接口名字，例如（sales_settlement_invoices）
        @param body: 请求参数，例如（{"request":{"sales_settlement_invoices":{"bill_date":"2024/8/6"}}}）
        """
        method = 'search_shop_supply_map'
        body = {"request":{"search_shop_supply_map":{"jingdong_sku":f"{sku}"}}}
        self.app_key = "xxxxxxxxxxxxxxxxxx"
        self.app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
        self.format = "json"
        self.method = method
        self.body = body
        self.timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.version = "2.0"
    @property
    def get_sign(self):

        sign_prama = f"{self.app_secret}app_key{self.app_key}format{self.format}method{self.method}timestamp{self.timestamp}version{self.version}{self.get_body}{self.app_secret}"
        sign_md5 = self.md5_hash(sign_prama)
        return sign_md5

    def md5_hash(self,key):
        md5_object = hashlib.md5()
        md5_object.update(key.encode("utf-8"))
        to_md5 = md5_object.hexdigest().upper()
        return to_md5
    @property
    def get_body(self):
        return json.dumps(self.body)
    @property
    def request_url(self):
        return f"http://1xx.xx.xx.xx:2024/WebApi/DoWebApi?app_key={self.app_key}&format=json&method={self.method}&timestamp={self.timestamp}&version={self.version}&sign={self.get_sign}"

class get_SearchShopSupplyMap_datas(SearchShopSupplyMap):
    def __init__(self,sku):
        super().__init__(sku)

    def execute(self):
        response = requests.post(self.request_url,data=self.get_body)
        if response.status_code == 200:
            response_datas = response.json()
            # print(response_datas)
            response_messages = response_datas["response"]["search_shop_supply_maps"]["search_shop_supply_map"]
            return response_messages
        else:
            return("Request failed with status code:", response.status_code)